package daoimpl

import (
	_ "github.com/go-sql-driver/mysql"
	"github.com/go-xorm/xorm"
	"log"
	"ustcLab/conf"
)

/*
	TODO:
	xorm base类

	cmd命令：
		xorm reverse mysql root:123456@tcp({ip:port})/ai_flow?charset=utf8 {输出的go模板文件目录}
*/

var engine *xorm.Engine

func init() {

	// TODO: Redis Init

	// MySQL Init
	var err error
	engine, err = xorm.NewEngine("mysql", conf.DataSourceName)
	if err != nil {
		log.Fatalln("[xorm] engine init err:", err)
	}
	if err = engine.Ping(); err != nil {
		log.Fatalln("[xorm] ping mysql failed, err: ", err)
	}
	log.Println("[xorm] link to mysql success.")

}

func GetInstance() *xorm.Engine {
	return engine
}

func Ping() {
	err := engine.Ping()
	if err != nil {
		log.Fatalln(err)
	}
}
